﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;

namespace PresentationLayer
{
    public partial class PurchaseOrderConfirmation : System.Web.UI.Page
    {
        ApprovePurchaseOrderControl control;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                #region Check if user exists and has permission to view this page
                if (Session["UserLoggedIn"] != null)
                {
                    List<string> list = Session["UserLoggedIn"] as List<string>;
                    if (list[0].Equals("validuser"))
                    {
                        int roleId = Convert.ToInt32(list[6]);
                        if (roleId < 6 || roleId > 7)
                        {
                            Response.Redirect("/AccessDenied.aspx");
                        }
                    }
                    else
                    {
                        Response.Redirect("/Login.aspx");
                    }
                }
                else
                {
                    Response.Redirect("/Login.aspx");
                }
                #endregion

                control = new ApprovePurchaseOrderControl();

                if (!IsPostBack)
                {
                    DisplayMessage();
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void DisplayMessage()
        {
            string poIdString = Session["PODetail"] as string;
            string result = Session["POResult"] as string;
            if (poIdString != null || result != null)
            {
                lblError.Text = "";
                if (result == "Approved")
                {
                    lblMessage.Text = "The purchase order (Purchase Order No: " + poIdString + ") has been approved.";
                    lblEmail.Text = "An email has been sent to the supplier";
                    Session.Remove("PODetail");
                    Session.Remove("POResult");
                }
                else if (result == "Rejected")
                {
                    lblMessage.Text = "The purchase order (Purchase Order No: " + poIdString + ") has been rejected.";
                    lblEmail.Text = "An email has been sent to the clerk";
                    Session.Remove("PODetail");
                    Session.Remove("POResult");
                }
            }
            else
            {
                lblMessage.Text = "";
                lblEmail.Text = "";
                lblError.Text = "No purchase order has been selected";
            }   
        }

        protected void btnReturnToList_Click(object sender, EventArgs e)
        {
            try
            {
                string redirectURL = control.RedirectToPurchaseOrderListForApproval();
                Response.Redirect(redirectURL);
            }
            catch (Exception ex)
            {
            }
        }

    }
}